package cn.demo.common.core.domain;

import cn.demo.common.constant.HttpStatus;
import lombok.Data;

import java.util.LinkedHashMap;

/**
 * 响应信息主体
 *
 * @author ruoyi
 */
@Data
public class MapR<T> extends LinkedHashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    /**
     * 成功
     */
    public static final int SUCCESS = HttpStatus.SUCCESS;

    /**
     * 失败
     */
    public static final int FAIL = HttpStatus.ERROR;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";


    public static <T> MapR<T> ok() {
        return restResult(null, SUCCESS, "操作成功");
    }

    public static <T> MapR<T> ok(T data) {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> MapR<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> MapR<T> fail() {
        return restResult(null, FAIL, "操作失败");
    }

    public static <T> MapR<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> MapR<T> fail(T data) {
        return restResult(data, FAIL, "操作失败");
    }

    public static <T> MapR<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> MapR<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    static <T> MapR<T> restResult(T data, int code, String msg) {
        MapR<T> apiResult = new MapR<>();
        apiResult.put(CODE_TAG,code);
        apiResult.put(DATA_TAG,data);
        apiResult.put(MSG_TAG,msg);
        return apiResult;
    }

    public static <T> Boolean isError(R<T> ret) {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(R<T> ret) {
        return R.SUCCESS == ret.getCode();
    }
}
